package com.ruoyi.personnel.salary.payroll.service;

import com.ruoyi.personnel.salary.hourly.mapper.HourlyMapper;
import com.ruoyi.personnel.salary.monthly.mapper.MonthlyMapper;
import com.ruoyi.personnel.salary.payroll.domain.PayrollCond;
import com.ruoyi.personnel.salary.payroll.domain.PayrollDomain;
import com.ruoyi.personnel.salary.payroll.domain.PayrollDto;
import com.ruoyi.personnel.salary.payroll.mapper.PayrollMapper;
import com.ruoyi.personnel.salary.piece.domain.PieceDto;
import com.ruoyi.personnel.salary.piece.mapper.PieceMapper;
import com.ruoyi.personnel.salary.tax.service.ITaxService;
import com.ruoyi.personnel.salary.utils.SalaryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务处理层
 * 工资表
 */
@Service
public class PayrollServiceImpl implements IPayrollService{
    @Autowired
    private PayrollMapper payrollMapper;
    @Autowired
    private MonthlyMapper monthlyMapper; //月薪
    @Autowired
    private HourlyMapper hourlyMapper; //计时工资
    @Autowired
    private PieceMapper pieceMapper; //计件工资
    @Autowired
    private ITaxService taxService; // 工资所得税

    /**
     * 根据搜索条件所有工资表
     * @param cond
     * @return
     */
    @Override
    public List<PayrollDto> selectPayrollList(PayrollCond cond) {
        List<PayrollDto> payrollDtos = payrollMapper.selectPayrollList(cond);
        for (int i = 0; i < payrollDtos.size(); i++) {
            // 获得当前员工的月薪
//            Double monthlySalary;
//            if (monthlyMapper.getMonthlySalary(payrollDtos.get(i).getStaffId()) == null) {
//                monthlySalary = 0.00;
//            } else {
//                monthlySalary = monthlyMapper.getMonthlySalary(payrollDtos.get(i).getStaffId());
//            }
//            payrollDtos.get(i).setMonthlySalary(monthlySalary);
            Double monthlySalary = SalaryUtil.isNull(monthlyMapper.getMonthlySalary(payrollDtos.get(i).getStaffId()));
            payrollDtos.get(i).setMonthlySalary(monthlySalary);
//            System.out.println("月薪" + monthlySalary);

            // 获得当前员工的计时工资
//            Double hourlyWage;
//            if (hourlyMapper.getHourlyWage(payrollDtos.get(i).getStaffId()) == null) {
//                hourlyWage = 0.00;
//            } else {
//                hourlyWage = hourlyMapper.getHourlyWage(payrollDtos.get(i).getStaffId());
//            }
            Double hourlyWage = SalaryUtil.isNull(hourlyMapper.getHourlyWage(payrollDtos.get(i).getStaffId()));
            payrollDtos.get(i).setHourlyWage(hourlyWage);
//            System.out.println("计时工资" + hourlyWage);

            // 获取当前员工的计件工资
            Double pieceRate;
            if (pieceMapper.selectPieceById(payrollDtos.get(i).getStaffId()) == null) {
                pieceRate = 0.00;
            } else {
                PieceDto pieceDto = pieceMapper.selectPieceById(payrollDtos.get(i).getStaffId());
                pieceRate = pieceDto.getQualifiedPrice() - pieceDto.getUnqualifiedPrice();
            }
            payrollDtos.get(i).setPieceRate(pieceRate);
//            System.out.println("计件工资" + pieceRate);

            // 计算工资总额，标准工资+津贴+月薪+计时工资+计件工资-请假扣除
            Double totalSalary = payrollDtos.get(i).getStandardWage() + payrollDtos.get(i).getAllowance() + monthlySalary + hourlyWage + pieceRate - payrollDtos.get(i).getLeaveDeduction();
            payrollDtos.get(i).setTotalSalary(totalSalary);

            // 计算应发工资，工资总额-扣住房基金-扣养老基金-扣医疗基金
            Double shouldSalary = totalSalary - payrollDtos.get(i).getHousingFund() - payrollDtos.get(i).getPensionFund() - payrollDtos.get(i).getMedicalFund();
            payrollDtos.get(i).setShouldSalary(shouldSalary);

            // 计算扣税值
            Double incomeTax = taxService.calculateIncomeTax(shouldSalary);
            payrollDtos.get(i).setIncomeTax(incomeTax);

            // 计算实际工资
            Double actualSalary = shouldSalary - incomeTax;
            payrollDtos.get(i).setActualSalary(actualSalary);
        }
        return payrollDtos;
    }

    /**
     * 根据员工编号搜索具体的工资表
     * @param staffId
     * @return
     */
    @Override
    public PayrollDto selectPayrollById(String staffId) {
        return payrollMapper.selectPayrollById(staffId);
    }

    /**
     * 新增工资表
     * @param payroll
     * @return
     */
    @Override
    public int insertPayroll(PayrollDomain payroll) {
        return payrollMapper.insertPayroll(payroll);
    }

    /**
     * 更新工资表
     * @param payroll
     * @return
     */
    @Override
    public int updatePayroll(PayrollDomain payroll) {
        return payrollMapper.updatePayroll(payroll);
    }

    /**
     * 删除员工工资表
     * 仅修改员工删除标志
     * @param payroll
     * @return
     */
    @Override
    public int updatePayrollDeleteFlag(PayrollDomain payroll) {
        return payrollMapper.updatePayrollDeleteFlag(payroll);
    }
}
